package end.util.jedis;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.shiro.cache.Cache;
import org.apache.shiro.cache.CacheException;
import org.apache.shiro.cache.CacheManager;

/**
 * 
 * ShiroRedisCacheManager 下午10:15:21 Copyright zhaocj Inc. All rights reserved.
 * Love Me Like Love Justin Bieber
 * 
 */
public class ShiroRedisCacheManager implements CacheManager {
	private static final Logger logger = LogManager
			.getLogger(ShiroRedisCacheManager.class);
	private final ConcurrentMap<String, Cache> caches = new ConcurrentHashMap<String, Cache>();
	private ShiroRedisWorker shiroRedisWorker;
	private String keyPrefix = "shiro_redis_cache:";

	public ShiroRedisWorker getShiroRedisWorker() {
		return shiroRedisWorker;
	}

	public void setShiroRedisWorker(ShiroRedisWorker shiroRedisWorker) {
		this.shiroRedisWorker = shiroRedisWorker;
	}

	public String getKeyPrefix() {
		return keyPrefix;
	}

	public void setKeyPrefix(String keyPrefix) {
		this.keyPrefix = keyPrefix;
	}

	@Override
	public <K, V> Cache<K, V> getCache(String name) throws CacheException {
		if (logger.isDebugEnabled()) {
			logger.debug("获取名称为: " + name + " 的RedisCache实例");
		}
		Cache c = caches.get(name);
		if (c == null) {
			c = new ShiroRedisCache<K, V>(shiroRedisWorker, keyPrefix);
			caches.put(name, c);
		}
		return c;
	}

}